package com.cutepeer.notes.adapters;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cutepeer.lib.utils.TextUtils;
import com.cutepeer.lib.view.Animations;
import com.cutepeer.lib.view.CustomToast;
import com.cutepeer.notes.R;
import com.cutepeer.notes.adapters.items.NoteItem;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class NoteAdapter extends ArrayAdapter<NoteItem> {

	private List<NoteItem> arrayList = new ArrayList<NoteItem>();
	private Activity mActivity;

	@Override
	public void add(NoteItem object) {
		arrayList.add(object);
		super.add(object);
	}

	public NoteAdapter(Activity a) {
		super(a, 0);
		mActivity = a;
	}

	public int getCount() {
		return this.arrayList.size();
	}

	public NoteItem getItem(int index) {
		return this.arrayList.get(index);
	}

	@SuppressLint("DefaultLocale")
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.ui_list_item_note, parent, false);
		TextView tvNoteIcon = (TextView) v.findViewById(R.id.tvNoteIcon);
		TextView tvNoteText = (TextView) v.findViewById(R.id.tvNoteText);
		TextView tvNoteTime = (TextView) v.findViewById(R.id.tvNoteTime);
		final Button btOptions = (Button) v.findViewById(R.id.btOptions);
		final RelativeLayout rlOptions = (RelativeLayout) v.findViewById(R.id.rlOptions);
		final Button btOptionCopy = (Button) v.findViewById(R.id.btOptionCopy);

		final NoteItem item = getItem(position);
		tvNoteIcon.setText(item.noteText.substring(0, 1).toUpperCase());
		tvNoteText.setText(Html.fromHtml(item.noteText).toString());
		tvNoteTime.setText(item.noteTime);


		btOptions.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Animations.vAnimationClick(btOptions);
				if (rlOptions.getVisibility() == View.GONE) {
					rlOptions.setVisibility(View.VISIBLE);
				} else {
					rlOptions.setVisibility(View.GONE);
				}
			}
		});

		btOptionCopy.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Animations.vAnimationClick(btOptionCopy);
				rlOptions.setVisibility(View.GONE);
				vCopy(mActivity, item.noteText);
			}
		});


		return v;
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static void vCopy(Activity mActivity, String mStr) {
		try {
			if (!TextUtils.isStringNull(mStr)) {
				int sdk = android.os.Build.VERSION.SDK_INT;
				if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
					android.text.ClipboardManager clipboard = (android.text.ClipboardManager) mActivity
							.getSystemService(Context.CLIPBOARD_SERVICE);
					clipboard.setText(mStr);
				} else {
					android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mActivity
							.getSystemService(Context.CLIPBOARD_SERVICE);
					android.content.ClipData clip = android.content.ClipData
							.newPlainText(
									mActivity.getResources().getString(
											R.string.appName), mStr);
					clipboard.setPrimaryClip(clip);
				}
				CustomToast.vToastShort(mActivity, mActivity.getString(R.string.toastCopied));
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}